﻿using Infragistics.Win.UltraWinToolbars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KD.Sms.UI_Query
{
    public partial class frmSamplingInfoSimple : Form
    {
        private DataTable E_Source = null;
        public frmSamplingInfoSimple()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tools_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            if (e.Tool.Key == "Query")
            {
                //E_Source = DAL.SqlLite.Main.Instance.DoQuerySamplingInfoSimple(dtBegin.Value.ToString("yyyy-MM-dd HH:mm:ss"), dtEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")
                //    , txtMaterial.SelectedIndex < 0 ? string.Empty : txtMaterial.SelectedValue.ToString()
                //    , txtSupplier.SelectedIndex < 0 ? string.Empty : txtSupplier.SelectedValue.ToString()
                //    , txtState.SelectedIndex < 0 ? string.Empty : txtState.Text.Trim());
                E_Source = DAL.SqlLite.Main.Instance.DoQuerySamplingInfoSimple(dtBegin.Value.ToString("yyyy-MM-dd HH:mm:ss"), dtEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")
                            , txtMaterial.Text.Trim()
                            , txtSupplier.Text.Trim()
                            , txtState.SelectedIndex < 0 ? string.Empty : txtState.Text.Trim());
                if (E_Source != null && E_Source.Rows.Count > 0)
                {
                    int _iStep = 1;
                    foreach (DataRow dr in E_Source.Rows)
                    {
                        dr["Row_Number"] = _iStep;
                        _iStep++;
                    }
                }
                gridMain.DataSource = E_Source;
            }
            else if (e.Tool.Key == "OutPut")
            {
                DoOutPut();
            }
            else if (e.Tool.Key == "Clear")
            {
                txtMaterial.SelectedIndex = -1;
                txtSupplier.SelectedIndex = -1;
                dtBegin.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
                dtEnd.Value = DateTime.Now.AddSeconds(5);
                txtState.SelectedIndex = 1;
            }
        }

        private void frmSamplingInfo_Load(object sender, EventArgs e)
        {
            dtBegin.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
            dtEnd.Value = DateTime.Now.AddSeconds(5);

            txtMaterial.DataSource = DAL.SqlLite.Main.Instance.DoQueryMaterial();
            txtMaterial.DisplayMember = "Material_Name";
            txtMaterial.ValueMember = "Material_id";
            txtMaterial.SelectedIndex = -1;

            txtSupplier.DataSource = DAL.SqlLite.Main.Instance.DoQuerySupplier();
            txtSupplier.DisplayMember = "Supplier_Name";
            txtSupplier.ValueMember = "Supplier_Id";
            txtSupplier.SelectedIndex = -1;

            txtState.SelectedIndex = 1;

            #region 根据权限 隐藏/显示供应商关键信息

            Column3.Visible = CacheUser.E_KeyInformation;
            Column4.Visible = CacheUser.E_KeyInformation;
            Column5.Visible = CacheUser.E_KeyInformation;
            Column6.Visible = CacheUser.E_KeyInformation;
            List<ToolBase> prepareDeleteControls = new List<ToolBase>();
            if (!CacheUser.E_KeyInformation)
            {
                foreach (ToolBase tb in this.tools.Tools)
                {
                    if (tb.CaptionResolved.Contains("物料"))
                        prepareDeleteControls.Add(tb);
                    if (tb.CaptionResolved.Contains("供应商"))
                        prepareDeleteControls.Add(tb);
                    if (tb.Control != null)
                    {
                        if (tb.Control.Name == "txtMaterial")
                            prepareDeleteControls.Add(tb);
                        if (tb.Control.Name == "txtSupplier")
                            prepareDeleteControls.Add(tb);
                    }
                }
                prepareDeleteControls.ForEach(item =>
                {
                    this.tools.Tools.Remove(item);
                });
            }

            #endregion
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoOutPut()
        {
            SaveFileDialog sfExcel = new SaveFileDialog();
            sfExcel.Title = "另存为";
            sfExcel.FileName = string.Format("{0}{1}采样数据导出.xls", (string.IsNullOrEmpty(CacheUser.Chinese_name) ? "采样信息" : CacheUser.Chinese_name), DateTime.Now.ToString("yyMMdd")); //设置默认另存为的名字，可选
            sfExcel.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            sfExcel.AddExtension = true;
            if (sfExcel.ShowDialog() == DialogResult.OK)
            {
                DataTable _tabExcel = E_Source.Copy();
                int _iStep = 1;
                foreach (DataRow dr in _tabExcel.Rows)
                {
                    dr["Row_Number"] = _iStep;
                    _iStep++;
                }
                string[] strFields = { "Row_Number", "car_no", "samplingPos", "Pos_count", "SampleWeight", "supplier_name", "kind_Name", "Material_Name", "Sampling_Time", "Sampling_time_end", "Sampling_User" };
                string[] strFieldsName = { "序号", "车牌号", "布点信息", "采样点数", "样品重量", "供应商", "物料", "规格", "开始时间", "结束时间", "采样员" };

                var listFields = strFields.ToList<string>();
                var listFieldsName = strFieldsName.ToList<string>();

                #region 无权限用户显示 供应商关键信息
                if (!CacheUser.E_KeyInformation)
                {
                    listFields.Remove("supplier_name");
                    listFields.Remove("kind_Name");
                    listFields.Remove("Material_Name");
                    listFieldsName.Remove("供应商");
                    listFieldsName.Remove("物料");
                    listFieldsName.Remove("规格");
                }
                #endregion

                NPOIHelper.ExportExcel(_tabExcel, string.Empty, "采样信息", listFields.ToArray(), listFieldsName.ToArray(), "采样信息", sfExcel.FileName);
                MessageBox.Show("导出成功!");
            }

        }

        private void gridMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                frmImage obj = new frmImage(gridMain.Rows[e.RowIndex].Cells["Sampling_Id"].Value.ToString());
                obj.ShowDialog();
            }
        }

        private void frmSamplingInfoSimple_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
                this.Close();
        }
    }
}
